<template>
  <div class="domestic-companies">
    <van-cell class="title" icon="arrow-left" @click="$router.back()"></van-cell>
    <van-pull-refresh
      v-model="refreshing"
      @refresh="onRefresh"
      :success-text="refreshSuccessText"
      success-duration="1200"
    >
      <van-list
        v-model="loading"
        :finished="finished"
        :finished-text="finishedText"
        @load="onLoad"
        :error.sync="error"
        error-text="请求失败，点击重新加载"
      >
        <company-item
          v-for="(item, index) in companyList"
          :key="index"
          :company="item"
        ></company-item>
      </van-list>
    </van-pull-refresh>
  </div>
</template>

<script>
import { getCompany } from '@/api/home.js'
import CompanyItem from '@/components/company-item'

export default {
  name: 'Company-Select',
  data () {
    return {
      companyList: [],
      finished: false,
      // 刷新状态
      refreshing: false,
      error: false,
      // 刷新
      loading: false,
      // 下拉刷新成功文本提示
      refreshSuccessText: '刷新成功!',
      finishedText: ''

    }
  },
  watch: {

  },
  components: {
    CompanyItem
  },
  created () {
    this.onRefresh()
  },
  mounted () {

  },
  methods: {
    async onRefresh () {
      try {
        this.finishedText = '数据加载中......'
        const { data } = await getCompany()
        this.companyList = data
        this.refreshSuccessText = '刷新成功!'
        this.loading = false
        this.refreshing = false
        this.finishedText = '没有更多了'
      } catch (err) {
        this.refreshSuccessText = '刷新失败!'
        this.loading = false
        this.refreshing = false
      }
    },
    onLoad () {
      try {
        this.loading = false
        this.finished = true
      } catch (err) {
        this.error = true
        this.Loading = false
      }
    }
  }
}
</script>

<style scoped lang="less">
.domestic-companies {
  padding-bottom: 100px;
}
.van-cell.title {
  background-color: #3f51b5;
  color: #fff;
}
</style>
